<template>
  <div>
    <el-form-item label="配置信息"></el-form-item>
    <div>
      <div style="width: 100%;display: flex;justify-content: flex-end"><el-button
          type="primary" size="mini" @click="addItemRow">新增配置信息</el-button></div>
      <el-table
          :data="self0ptions.options.radioConfig"
          border
          style="width: 100%;margin-top: 5px"
      >
        <el-table-column
            prop="label"
            label="选项名称"
            align="center"
        >
        </el-table-column>
        <el-table-column
            align="center"
            v-if="self0ptions.options.sourceSelect == '每个选项可自定义分值' || self0ptions.options.sourceSelect == '扣分题(默认得总分)'
|| self0ptions.options.sourceSelect == '少选得部分分;错选得0分'"
            prop="source"
            key="source"
            width="80px"
            label="分数"
        >
          <template slot-scope="scope">
            <el-input v-model="scope.row.source"></el-input>
          </template>
        </el-table-column>
        <el-table-column
            align="center"
            v-if="self0ptions.options.sourceSelect == '此题有唯一答案和分值' || self0ptions.options.sourceSelect == '少选得部分分;错选得0分' || self0ptions.options.sourceSelect == '全部答对才得分'"
            prop="onlyAnswer"
            key="onlyAnswer"
            width="80px"
            :label="self0ptions.options.sourceSelect == '此题有唯一答案和分值' ? '唯一答案' : '正确答案'"
        >
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.onlyAnswer"
                active-color="#409eff"
                inactive-color="#909399"
            @change="changeOnly(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
            align="center"
            prop="handle"
            label="操作"
            width="80px"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="delItem(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
        title="新增配置"
        :visible.sync="dialogVisible"
        width="20%"
    >
      <el-form ref="refElForm" :model="formData" :rules="formDataRules" label-width="80px">
        <el-form-item label="选项名称" prop="label">
          <el-input v-model="formData.label" placeholder="请输入选项名称" clearable></el-input>
        </el-form-item>
      </el-form>
      <div style="margin-top: 20px;display: flex;justify-content: center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "radioConfig-editor",
  props:{
    self0ptions:{
      type:Object,
    }
  },
  data(){
    return {
      dialogVisible:false,
      formData:{
        label:'',
      },
      formDataRules:{
        label:[{required:true,message:'请填写选项名称',trigger:"blur"}],
      },
    }
  },
  methods:{
    delItem(row){
      this.$confirm('是否确认删除？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const index = this.$props.self0ptions.options.radioConfig.findIndex(item => item.label == row.label)
        if(index > -1){
          this.$props.self0ptions.options.radioConfig.splice(index,1)
          this.$message.success('删除成功')
          return
        }
      })
    },
    addItemRow(){
      this.dialogVisible = true
      this.formData.label = ''
    },
    submitForm(){
      this.$refs.refElForm.validate(bol=>{
        if(bol){
          this.$props.self0ptions.options.radioConfig.push({
            ...this.formData,
            source:0,
            onlyAnswer:false
          })
          this.$message.success('添加成功')
          this.dialogVisible = false
        }
      })
    },
    changeOnly(row){
      if(this.self0ptions.options.sourceSelect == '此题有唯一答案和分值'){
        if(row.onlyAnswer == true){
          this.self0ptions.options.radioConfig.forEach(item => {
            item.onlyAnswer = false
          })
          const ind = this.self0ptions.options.radioConfig.findIndex(item => item.label == row.label)
          if(ind > -1){
            this.$props.self0ptions.options.radioConfig[ind].onlyAnswer = true
          }
        }
      }
    }
  }
}
</script>
<style scoped lang="scss">

</style>